
;;=========================================================================
;; Routine for converting BOTH Wayne's CSV file containing HHT
;; observed locations and the new Spring 2011 data set into a single
;; IDL save file: HHT_ASSOC.sav for associating the treasure trove of
;; spectra with BGPS Bolocat sources.

PRO ASSOCIATE_HHT_BOLOCAT

  ;; Announce what we're doing
  message,'Running association between HHT spectra and Bolocat...',/inf
  
  ;; Read in Wayne's CSV file & count lines
  readcol,'local/BGPS_LIST.table',$
          name,l,b,hflag,hvel,nflag,nvel,dnear,dfar,irdcflag,$
          skipline=1,format="(a,d,d,i,d,i,d,d,d,i)"
  nobs = n_elements(name)
  
  ;; Make a list of the HCO+ spectra from the Spring 2011 observing run
  hcop_list = FILE_SEARCH('./local/hht_2011/*HCOPwide.fits',COUNT=ns11)
  
  ;; Create the HHT structure & read in the BGPS CSV file
  hht = REPLICATE( CREATE_STRUCT('name','','glon',double(0),'glat',double(0),$
                                 'cnum',long(0),'dist_bcat_peak',double(0),$
                                 'dist_bcat_cen',double(0)), nobs+ns11)
  
  s = read_bgps_csv('bgps_distance_database.csv',csv,/ver)
  n_miss = 0L
  
  pdist = dblarr(nobs)
  
  ;; Loop through HHT sources from Wayne's CSV file
  FOR i=0L, nobs-1 DO BEGIN
     
     ;; Read in directly from Wayne's file
     hht[i].name = name[i]
     hht[i].glon = l[i]
     hht[i].glat = b[i]
     
     ;; Associate with Bolocat v1.01
     gcirc, 2, l[i], b[i], s.glon_peak, s.glat_peak, dist_peak
     gcirc, 2, l[i], b[i], s.glon_cen,  s.glat_cen,  dist_cen
     
     pick_peak = WHERE( dist_peak EQ min(dist_peak), n_pp)
     pick_cen  = WHERE( dist_cen  EQ min(dist_cen),  n_pc)
     
     hht[i].dist_bcat_peak = min(dist_peak)
     hht[i].dist_bcat_cen  = min(dist_cen)
     
     ;; ONLY SELECT POINTINGS WITH POSITION within 15" of BGPS peak POSITION
     IF (hht[i].DIST_BCAT_PEAK LE 15.) THEN BEGIN
        hht[i].cnum = s[pick_peak].cnum
     ENDIF ELSE BEGIN
        pdist[i] = min(dist_peak)
        n_miss ++
     ENDELSE
     
  ENDFOR
  
  print,"Number of Wayne's pointings outside 0.5 beams: ",n_miss
  
  ;; Next, go through the Spring 2011 files and add them to the HHT structure
  
  n_miss = 0L
  
  FOR j=0L, ns11-1 DO BEGIN
     
     ;; Index the HHT structure with i to continue from above, while
     ;; counting the Spring 2011 data with j.
     i = nobs + j
     
     ;; Get pertinent information from the FITS header
     hdr = headfits(hcop_list[j])
     
     hht[i].name = strtrim(sxpar(hdr,'OBJECT'),2)
     ra  = sxpar(hdr,'CRVAL2')
     dec = sxpar(hdr,'CRVAL3')
     euler,ra, dec, l, b, 1
     hht[i].glon = l
     hht[i].glat = b
     
     ;; Associate with Bolocat v1.01
     gcirc, 2, l, b, s.glon_peak, s.glat_peak, dist_peak
     gcirc, 2, l, b, s.glon_cen,  s.glat_cen,  dist_cen
     
     pick_peak = WHERE( dist_peak EQ min(dist_peak), n_pp)
     pick_cen  = WHERE( dist_cen  EQ min(dist_cen),  n_pc)
     
     hht[i].dist_bcat_peak = min(dist_peak)
     hht[i].dist_bcat_cen  = min(dist_cen)
     
     ;; Check how close the object is to a BOLOCAT v1.0.1 position
     ;; Since we redid this, we sould be within 2 beams, easily.
     ;; ONLY SELECT POINTINGS WITH POSITION within 15" of BGPS peak POSITION
     IF (hht[i].DIST_BCAT_PEAK LE 15.) THEN BEGIN
        
        hht[i].cnum = s[pick_peak].cnum
        
     ENDIF ELSE BEGIN
        print,string(s[pick_peak].cnum,min(dist_peak),s[pick_cen].cnum, $
                     min(dist_cen), dist_cen[pick_peak],format=$
                     "('Peak: ',I4,' ',F6.1,'  Cen: ',I4,' ',F6.1,' ',F6.1)")
        n_miss ++
        
     ENDELSE
     
  ENDFOR
  print,"Number of Spring 2011 pointings outside 0.5 beams: ",n_miss
  
  save,hht,filename='local/HHT_ASSOC.sav',/ver
  
END
